<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 危险权限请求获取到Permission Group中的任一权限，则该组内其他权限自动被允许.">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-权限管理">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android/Android-权限管理/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 危险权限请求获取到Permission Group中的任一权限，则该组内其他权限自动被允许.">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-fa2bc157153ee2fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-1fe7c533de2db291.gif?imageMogr2/auto-orient/strip">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/3110861-19890ce221d3a7fd.gif?imageMogr2/auto-orient/strip">
<meta property="og:updated_time" content="2018-08-06T11:22:42.556Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-权限管理">
<meta name="twitter:description" content="####1. 危险权限请求获取到Permission Group中的任一权限，则该组内其他权限自动被允许.">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/3110861-fa2bc157153ee2fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android/Android-权限管理/"/>





  <title>Android-权限管理 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android/Android-权限管理/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-权限管理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 危险权限<br><img src="https://upload-images.jianshu.io/upload_images/3110861-fa2bc157153ee2fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"><br><strong>请求获取到Permission Group中的任一权限，则该组内其他权限自动被允许.</strong></p>
<a id="more"></a>
<p>####2. 系统Api使用</p>
<p>######1). 创建界面布局<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;android.support.constraint.ConstraintLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;</span><br><span class="line">    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    tools:context=&quot;com.mazaiting.permissiondemo.MainActivity&quot;&gt;</span><br><span class="line"></span><br><span class="line">  &lt;Button</span><br><span class="line">      android:onClick=&quot;takePicture&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:text=&quot;拍照&quot;</span><br><span class="line">      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;</span><br><span class="line">      app:layout_constraintLeft_toLeftOf=&quot;parent&quot;</span><br><span class="line">      app:layout_constraintRight_toRightOf=&quot;parent&quot;</span><br><span class="line">      app:layout_constraintTop_toTopOf=&quot;parent&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;/android.support.constraint.ConstraintLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>######2). 点击按钮打开相机<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 拍照按钮</span><br><span class="line"> */</span><br><span class="line">fun takePicture(view: View) &#123;</span><br><span class="line">  // 检测权限</span><br><span class="line">  if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">    // 用户拒绝了权限,并且点击了不再提醒</span><br><span class="line">    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) &#123;</span><br><span class="line">      // 已经禁止提示</span><br><span class="line">      AlertDialog.Builder(this)</span><br><span class="line">              .setTitle(&quot;提示&quot;)</span><br><span class="line">              .setMessage(&quot;权限已拒绝，是否需要重新开启&quot;)</span><br><span class="line">              .setPositiveButton(&quot;确定&quot;) &#123; dialog: DialogInterface?, which: Int -&gt;</span><br><span class="line">                // 设置防止出现不再提示页面,进入权限管理页面</span><br><span class="line">                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)</span><br><span class="line">                val uri = Uri.fromParts(&quot;package&quot;, packageName, null)</span><br><span class="line">                intent.data = uri</span><br><span class="line">                startActivityForResult(intent, REQUEST_PERMISSION_SETTING)</span><br><span class="line">              &#125;</span><br><span class="line">              .setNegativeButton(&quot;取消&quot;, null)</span><br><span class="line">              .create()</span><br><span class="line">              .show()</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      // 无权限</span><br><span class="line">      ActivityCompat.requestPermissions(</span><br><span class="line">              this,</span><br><span class="line">              arrayOf(Manifest.permission.CAMERA),</span><br><span class="line">              REQUEST_PERMISSION</span><br><span class="line">      )</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; else &#123;</span><br><span class="line">    // 有权限</span><br><span class="line">    openCamera()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 打开相机</span><br><span class="line"> */</span><br><span class="line">private fun openCamera() &#123;</span><br><span class="line">  val openCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)</span><br><span class="line">  startActivity(openCameraIntent)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). 权限结果回调<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">override fun onRequestPermissionsResult(requestCode: Int, permissions: Array&lt;out String&gt;, grantResults: IntArray) &#123;</span><br><span class="line">  super.onRequestPermissionsResult(requestCode, permissions, grantResults)</span><br><span class="line">  // 判断请求码</span><br><span class="line">  if (REQUEST_PERMISSION == requestCode) &#123;</span><br><span class="line">    // 判断grantResults数组不为空</span><br><span class="line">    if (grantResults.isNotEmpty() &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">      // 用户同意授权</span><br><span class="line">      openCamera()</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      // 用户拒绝授权</span><br><span class="line">      Toast.makeText(this, &quot;用户拒绝了授权&quot;, Toast.LENGTH_SHORT).show()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######4). 完整代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">class MainActivity1: AppCompatActivity() &#123;</span><br><span class="line">  </span><br><span class="line">  // 存储静态量</span><br><span class="line">  companion object &#123;</span><br><span class="line">    /**权限请求*/</span><br><span class="line">    val REQUEST_PERMISSION = 0x100</span><br><span class="line">    /**权限设置页返回*/</span><br><span class="line">    val REQUEST_PERMISSION_SETTING = 0x101</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  override fun onCreate(savedInstanceState: Bundle?) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState)</span><br><span class="line">    setContentView(R.layout.activity_main)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 拍照按钮</span><br><span class="line">   */</span><br><span class="line">  fun takePicture(view: View) &#123;</span><br><span class="line">    // 检测权限</span><br><span class="line">    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">      // 用户拒绝了权限,并且点击了不再提醒</span><br><span class="line">      if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) &#123;</span><br><span class="line">        // 已经禁止提示</span><br><span class="line">        AlertDialog.Builder(this)</span><br><span class="line">                .setTitle(&quot;提示&quot;)</span><br><span class="line">                .setMessage(&quot;权限已拒绝，是否需要重新开启&quot;)</span><br><span class="line">                .setPositiveButton(&quot;确定&quot;) &#123; dialog: DialogInterface?, which: Int -&gt;</span><br><span class="line">                  // 设置防止出现不再提示页面,进入权限管理页面</span><br><span class="line">                  val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)</span><br><span class="line">                  val uri = Uri.fromParts(&quot;package&quot;, packageName, null)</span><br><span class="line">                  intent.data = uri</span><br><span class="line">                  startActivityForResult(intent, REQUEST_PERMISSION_SETTING)</span><br><span class="line">                &#125;</span><br><span class="line">                .setNegativeButton(&quot;取消&quot;, null)</span><br><span class="line">                .create()</span><br><span class="line">                .show()</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        // 无权限</span><br><span class="line">        ActivityCompat.requestPermissions(</span><br><span class="line">                this,</span><br><span class="line">                arrayOf(Manifest.permission.CAMERA),</span><br><span class="line">                REQUEST_PERMISSION</span><br><span class="line">        )</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      // 有权限</span><br><span class="line">      openCamera()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 打开相机</span><br><span class="line">   */</span><br><span class="line">  private fun openCamera() &#123;</span><br><span class="line">    val openCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)</span><br><span class="line">    startActivity(openCameraIntent)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  override fun onRequestPermissionsResult(requestCode: Int, permissions: Array&lt;out String&gt;, grantResults: IntArray) &#123;</span><br><span class="line">    super.onRequestPermissionsResult(requestCode, permissions, grantResults)</span><br><span class="line">    // 判断请求码</span><br><span class="line">    if (REQUEST_PERMISSION == requestCode) &#123;</span><br><span class="line">      // 判断grantResults数组不为空</span><br><span class="line">      if (grantResults.isNotEmpty() &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">        // 用户同意授权</span><br><span class="line">        openCamera()</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        // 用户拒绝授权</span><br><span class="line">        Toast.makeText(this, &quot;用户拒绝了授权&quot;, Toast.LENGTH_SHORT).show()</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######5). 示例效果<br><img src="https://upload-images.jianshu.io/upload_images/3110861-1fe7c533de2db291.gif?imageMogr2/auto-orient/strip" alt="图2.gif"></p>
<p>####3. <a href="https://github.com/tbruyelle/RxPermissions" target="_blank" rel="noopener">RxPermissions</a></p>
<p>######1). 添加依赖<br><a href="https://github.com/ReactiveX/RxAndroid" target="_blank" rel="noopener">RxAndroid</a><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// 动态权限管理</span><br><span class="line">implementation &apos;com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar&apos;</span><br><span class="line">implementation &apos;io.reactivex.rxjava2:rxandroid:2.0.2&apos;</span><br></pre></td></tr></table></figure></p>
<p>######2). 示例代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">class MainActivity : AppCompatActivity() &#123;</span><br><span class="line">  // 定义权限类</span><br><span class="line">  private lateinit var mRxPermission: RxPermissions</span><br><span class="line">  </span><br><span class="line">  // 存储静态量</span><br><span class="line">  companion object &#123;</span><br><span class="line">    /**权限设置页返回*/</span><br><span class="line">    val REQUEST_PERMISSION_SETTING = 0x101</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  override fun onCreate(savedInstanceState: Bundle?) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState)</span><br><span class="line">    setContentView(R.layout.activity_main)</span><br><span class="line">    mRxPermission = RxPermissions(this)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 拍照按钮</span><br><span class="line">   */</span><br><span class="line">  fun takePicture(view: View) &#123;</span><br><span class="line">    mRxPermission</span><br><span class="line">            .requestEachCombined(</span><br><span class="line">                    Manifest.permission.CAMERA,</span><br><span class="line">                    Manifest.permission.WRITE_EXTERNAL_STORAGE</span><br><span class="line">            )</span><br><span class="line">            .subscribe &#123; permission -&gt;</span><br><span class="line">              when &#123;</span><br><span class="line">                permission.granted -&gt; openCamera()</span><br><span class="line">                permission.shouldShowRequestPermissionRationale -&gt; showSetting()</span><br><span class="line">                else -&gt; &#123;</span><br><span class="line">                  Toast.makeText(this, &quot;被拒绝&quot;, Toast.LENGTH_SHORT).show()</span><br><span class="line">                  showSetting()</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 显示权限设置页</span><br><span class="line">   */</span><br><span class="line">  private fun showSetting() &#123;</span><br><span class="line">    // 已经禁止提示</span><br><span class="line">    AlertDialog</span><br><span class="line">            .Builder(this)</span><br><span class="line">            .setTitle(&quot;提示&quot;)</span><br><span class="line">            .setMessage(&quot;权限已拒绝，是否需要重新开启&quot;)</span><br><span class="line">            .setPositiveButton(&quot;确定&quot;) &#123; dialog: DialogInterface?, which: Int -&gt;</span><br><span class="line">              // 设置防止出现不再提示页面,进入权限管理页面</span><br><span class="line">              val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)</span><br><span class="line">              val uri = Uri.fromParts(&quot;package&quot;, packageName, null)</span><br><span class="line">              intent.data = uri</span><br><span class="line">              startActivityForResult(intent, REQUEST_PERMISSION_SETTING)</span><br><span class="line">            &#125;</span><br><span class="line">            .setNegativeButton(&quot;取消&quot;, null)</span><br><span class="line">            .create()</span><br><span class="line">            .show()</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * 打开相机</span><br><span class="line">   */</span><br><span class="line">  private fun openCamera() &#123;</span><br><span class="line">    val openCameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)</span><br><span class="line">    startActivity(openCameraIntent)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>######3). 效果演示<br><img src="https://upload-images.jianshu.io/upload_images/3110861-19890ce221d3a7fd.gif?imageMogr2/auto-orient/strip" alt="图3.gif"></p>
<p>####<a href="https://gitee.com/mazaiting/Article/tree/master/PermissionDemo" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-文件路径/" rel="next" title="Android-文件路径">
                <i class="fa fa-chevron-left"></i> Android-文件路径
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-自定义注解之源码注解(RetentionPolicy-SOURCE)/" rel="prev" title="Android-自定义注解之源码注解(RetentionPolicy-SOURCE)">
                Android-自定义注解之源码注解(RetentionPolicy-SOURCE) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
